package com.zzw.controller;

import com.zzw.bean.Msg;
import com.zzw.bean.ProductES;
import com.zzw.service.ProductESService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/productES")
public class ProductESController {

    @Autowired
    private ProductESService productESService;

    /**
     * 跳转到商品搜索页面
     */
    @GetMapping("/search")
    public String search() {
        return "product/search";
    }

    /**
     * 创建商品索引
     */
    @ResponseBody
    @PostMapping("/createIndex")
    public Msg createIndex() {
        try {
            boolean result = productESService.createProductIndex();
            if (result) {
                return Msg.success().add("message", "创建索引成功");
            } else {
                return Msg.fail("创建索引失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return Msg.fail("创建索引异常: " + e.getMessage());
        }
    }

    /**
     * 删除商品索引
     */
    @ResponseBody
    @DeleteMapping("/deleteIndex")
    public Msg deleteIndex() {
        try {
            boolean result = productESService.deleteProductIndex();
            if (result) {
                return Msg.success().add("message", "删除索引成功");
            } else {
                return Msg.fail("删除索引失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return Msg.fail("删除索引异常: " + e.getMessage());
        }
    }

    /**
     * 同步所有商品数据到ES
     */
    @ResponseBody
    @PostMapping("/syncData")
    public Msg syncData() {
        try {
            boolean result = productESService.syncProductData();
            if (result) {
                return Msg.success().add("message", "同步数据成功");
            } else {
                return Msg.fail("同步数据失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return Msg.fail("同步数据异常: " + e.getMessage());
        }
    }

    /**
     * 关键词搜索商品
     */
    @ResponseBody
    @GetMapping("/searchByKeyword")
    public Msg searchByKeyword(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        try {
            List<ProductES> list = productESService.searchByKeyword(keyword, pageNum, pageSize);
            return Msg.success().add("list", list);
        } catch (IOException e) {
            e.printStackTrace();
            return Msg.fail("搜索异常: " + e.getMessage());
        }
    }

    /**
     * 多条件搜索商品
     */
    @ResponseBody
    @GetMapping("/searchProduct")
    public Msg searchProduct(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "categoryId", required = false) Integer categoryId,
            @RequestParam(value = "minPrice", required = false) BigDecimal minPrice,
            @RequestParam(value = "maxPrice", required = false) BigDecimal maxPrice,
            @RequestParam(value = "sort", required = false) String sort,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        try {
            Map<String, Object> resultMap = productESService.searchProduct(
                    keyword, categoryId, minPrice, maxPrice, sort, pageNum, pageSize);
            return Msg.success().add("data", resultMap);
        } catch (IOException e) {
            e.printStackTrace();
            return Msg.fail("搜索异常: " + e.getMessage());
        }
    }

    /**
     * 获取相关商品推荐
     */
    @ResponseBody
    @GetMapping("/getRelatedProducts/{productId}")
    public Msg getRelatedProducts(
            @PathVariable Integer productId,
            @RequestParam(value = "size", defaultValue = "6") Integer size) {
        try {
            List<ProductES> list = productESService.getRelatedProducts(productId, size);
            return Msg.success().add("list", list);
        } catch (IOException e) {
            e.printStackTrace();
            return Msg.fail("获取相关商品异常: " + e.getMessage());
        }
    }

    /**
     * 获取热门商品推荐
     */
    @ResponseBody
    @GetMapping("/getHotProducts")
    public Msg getHotProducts(
            @RequestParam(value = "size", defaultValue = "6") Integer size) {
        try {
            List<ProductES> list = productESService.getHotProducts(size);
            return Msg.success().add("list", list);
        } catch (IOException e) {
            e.printStackTrace();
            return Msg.fail("获取热门商品异常: " + e.getMessage());
        }
    }
} 